<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header style="position: fixed; width: 100%; z-index: 9">
      <div>
        <img
          src="../assets/img/IT_white.png"
          style="
            margin-left: 30px;
            margin-right: 10px;
            width: 80px;
            height: 50px;
          "
          alt=""
        />
        <span></span>
      </div>

      <el-aside width="80%" style="transition: 0.2s ease">
        <el-menu
          :default-active="activePath"
          class="el-menu-demo"
          @open="handleOpen"
          @close="handleClose"
          text-color="#eaedf1"
          mode="horizontal"
          active-text-color="#ffffff"
          :unique-opened="true"
          :collapse-transition="false"
          :router="true"
        >
          <el-menu-item id="homebutt" index="/welcome" style="width: 120px">
            <i class="el-icon-s-home" style="color: #fff"></i>
            <span>首页</span>
          </el-menu-item>

          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <template slot="title">
              <i :class="item.icon" style="color: #fff"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!--二级菜单-->
            <!---router开启为true后，点击菜单跳转的位置为index中的路由-->
            <el-menu-item
              style="background-color: #fff; color: #000"
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <i :class="subItem.icon" style="color: #52b2cb"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>

          <el-menu-item
            v-if="loginUser.roles == 'ROLE_ADMIN'"
            id="homebutt"
            index="/config"
            style="width: 120px"
          >
            <i class="el-icon-s-operation" style="color: #fff"></i>
            <span>配置管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <div class="stuInfo">
        <div class="name">{{ loginUser.realName }}</div>
        <div class="stuId">{{ loginUser.studentId }}</div>
      </div>
      <div v-if="!isGuest" class="info">
        <!-- 下拉箭头 -->
        <el-dropdown @command="setDialogInfo">
          <span class="el-dropdown-link">
            <img id="head_logo" src="../assets/img/logo.png" alt="" srcset="" />
          </span>

          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="info">账号设置</el-dropdown-item>
            <!-- <el-dropdown-item command="editpswd">修改密码</el-dropdown-item> -->
            <el-dropdown-item command="logout" @click="logout"
              >退出登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div @click="toLogin" v-else class="info_login">
        <span class="login_label">登录</span>
      </div>

      <!-- <el-button type="info" @click="logout">退出</el-button> -->
    </el-header>
    <!--主体区域-->
    <el-container class="container">
      <!--主体区域-->
      <el-main id="bg" :style="{ 'background-size': '100% 100%' }">
        <!-- <el-main id="bg" > -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import menu from "./Home.js";
import Config from "../views/config.js";
export default {
  data() {
    return {
      menuList: [], //左侧菜单数据
      activePath: "", //激活菜单的值
      url: "",
      isGuest: true,
      loginUser: {
        roles: "ROLE_GUEST",
        realName: "",
        studentId: "",
      },
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
    this.isGuest = window.sessionStorage.getItem("isGuest") == "1";
    // console.log(this.isGuest);
    // this.loginRole = window.sessionStorage.getItem("role");
    if (window.sessionStorage.getItem("user") != null) {
      this.loginUser = JSON.parse(window.sessionStorage.getItem("user"));
    }
  },
  mounted() {
    this.initBgImg();
  },
  methods: {
    async initBgImg() {
      if (window.sessionStorage.getItem("bgUrl") != null) {
        this.url = window.sessionStorage.getItem("bgUrl");
        console.log("已从本地缓存获取背景图片地址", this.url);
      } else {
        try {
          const res = await this.$axios.get(
            `${this.HOST}/config/api/background`
          );
          console.log("获取背景图片成功", res.data.data);
          // window.sessionStorage.setItem("bgRequested", 1);
          let bgUrl = Config.picPrefixUrl + res.data.data.background;
          this.url = "url('" + bgUrl + "')";
          window.sessionStorage.setItem("bgUrl", this.url);
        } catch (err) {
          console.log("获取背景图片失败，使用备用默认图");
          this.url = "url('" + require("../assets/img/welcome_bg1.jpg") + "')";
          // console.log(this.url);
        }
      }
      document.getElementById("bg").style.backgroundsize = "100% 100%";
      document.getElementById("bg").style.backgroundImage = this.url;

      // console.log("bg", document.getElementById('bg').style.backgroundImage);
    },
    /**
     * 监听点击
     */
    setDialogInfo(cmdItem) {
      switch (cmdItem) {
        case "info":
          this.setInfo();
          break;
        case "editpswd":
          this.editpswd();
          break;
        case "logout":
          this.logout();
          break;
      }
    },
    /**
     * 账号设置
     */
    setInfo() {
      // this.$router.replace("/");
      this.$router.push("/accountSetting");
    },
    /**
     * 修改密码
     */
    editpswd() {
      this.$router.push("/changePwd");
    },
    /**
     * 菜单折叠的切换
     */
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    /**
     * 获取左侧菜单
     */
    async getMenuList() {
      // const {
      //     data: res
      // } = await this.$http.get('menus')
      // if (res.meta.status !== 200) return this.$message({
      //     message: "获取菜单失败",
      //     type: "error"
      // });
      // this.menuList = res.data;
      //模拟获取数据
      this.menuList = menu.row;
    },
    /**
     * 退出操作
     */
    logout() {
      window.sessionStorage.clear();
      window.sessionStorage.setItem("isGuest", "1");
      this.$router.push("/");
    },
    /**
     * 保存连接点击状态
     */
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
    /**
     * 菜单展开
     */
    handleOpen(key, keyPath) {
      // console.log(key, keyPath);
    },
    /**
     * 菜单收起
     */
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    },
    toLogin() {
      this.$router.push("/");
    },
  },
};
</script>
<style lang="less" scoped>
// /deep/.el-submenu__title{
//   background-color: rgba(0,0,0,0) !important;
// }

/deep/.el-menu--popup {
  background-color: white !important;
}

/deep/.el-menu--popup :hover {
  background-color: rgba(255, 255, 255, 0.5) !important;
}

::-webkit-scrollbar {
  width: 0 !important;
  height: 0;
}

.el-menu-demo {
  background-color: rgba(0, 0, 0, 0) !important;
}

.el-menu-demo :hover {
  // font-weight: bold !important;
  color: white;
  background-color: transparent !important;
  transition: 0.5s ease !important;
}

// el-menu-item :hover {
//   background-color: transparent !important;
// }

.iconfont {
  margin-right: 10px;
}

#head_logo {
  width: 30px;
  height: 30px;
  border: white solid 2px;
  padding: 5px;
  border-radius: 50%;
}

#head_logo:hover {
  padding: 5px;
  border: rgba(255, 255, 255, 0.349) solid 2px;
  box-shadow: 0px 0px 5px rgba(7, 7, 7, 0.904);
  transition: ease 0.4s;
}

.home-container {
  // padding-top: 60px;
  padding-bottom: 0px;
  height: 100%;

  #home_icon {
    font-size: 15px;
    margin: 0px 30px;
  }

  .el-header {
    position: absolute;
    background-size: 100% 100%;
    background-image: url("../assets/img/bg.jpg");
    display: flex;
    justify-content: flex-start;
    padding-left: 0;
    align-items: center;
    color: #fff;

    div {
      display: flex;
      align-items: center;

      img {
        width: 100px;
        height: 100px;
      }

      span {
        margin-left: 5px;
      }
    }

    .info {
      margin-left: auto;
      img {
        width: 40px;
        height: 40px;
      }
      img:hover {
        cursor: pointer;
      }
    }

    .info_login {
      padding: 10px;
      margin-left: auto;
      // border:1px solid black;
      font-weight: bold;
      z-index: 10;

      .login_label:hover {
        color: blue;
        text-decoration: underline;
        cursor: pointer;
      }
    }

    .stuInfo {
      position: absolute;
      // padding-right: 10px;
      top: 10px;
      right: 80px;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      font-size: 14px;
      color: rgba(255, 255, 255, 0.9);

      .name {
        margin-left: auto;
        // width: 100%;
        text-align: end;
      }
    }
  }

  .container {
    // margin-bottom: 60px;
    .el-main {
      width: 100%;
      position: fixed;
      padding-top: 80px;
      padding-bottom: 40px;
      // margin-bottom: 60px;
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
      height: 100%;
      z-index: 0;
    }
    .el-main:after {
      position: fixed;
      top: 0;
      left: 0;
      content: "";
      background-color: rgb(0, 0, 0);
      opacity: 0.5;
      z-index: 1;
      width: 100%;
      height: 100%;
      z-index: -2;
    }
  }
}
</style>